package service

import "secondproject/model"

type CustomerService struct {
	customers []model.Customer
	// 人数，也用作id
	customerNum int
}

func NewCustomerService() *CustomerService {
	cts := &CustomerService{}
	cts.customerNum = 1
	customer1 := model.NewCustomer(1, "zw", "男",
		20, "13122223333", "zw@qq.com")
	// 初始化放入一个成员
	cts.customers = append(cts.customers, customer1)
	return cts
}

// 成员信息
func (this *CustomerService) List() []model.Customer {
	return this.customers
}

// 添加客户
func (this *CustomerService) Add(cus model.Customer) bool {
	this.customerNum++
	cus.Id = this.customerNum
	this.customers = append(this.customers, cus)

	return true
}

// 删除客户
func (this *CustomerService) Delete(id int) bool {
	index := this.FindById(id)
	if index == -1 {
		return false
	}
	// index 从0到index，不包括index
	this.customers = append(this.customers[:index], this.customers[index+1:]...)
	return true
}

// 查找客户index
func (this *CustomerService) FindById(id int) int {
	index := -1
	for i := 0; i < len(this.customers); i++ {
		if this.customers[i].Id == id {
			index = i
		}
	}
	return index
}
